layui.use(['table', 'admin', 'ax', 'func','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var form = layui.form;

    var lastday = 3;

    //当前月份
    var thisdate = new Date();
    if(thisdate.getDate()<lastday){
        thisdate.setMonth(thisdate.getMonth()-1);
    }
    let monthNum = thisdate.getMonth() + 1;
    const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);

    var buttonValid = false;

    //渲染时间选择框
    laydate.render({
        elem: "#month",
        value: month,
        type: 'month',
        tiger: 'click'
    });

    /**
     * 展示有可能影响考勤数据的信息，
     */
    var AttendAuditBtyAreaImpact = {
        tableId: "attendAuditBtyAreaImpactTable"
    };


    // private String bh;
    // private Integer id;
    // private String bz;
    // private Integer flag;
    // private Integer flagd;
    // private Integer qtgz;
    // private String qt;
    // private String dz;
    // private String dh;
    // private Integer aId;
    // private String aBh;
    // private Date rq;
    // private String openid;
    // private String bank;
    // private String hh;

    /**
     * 初始化表格的列
     */
    AttendAuditBtyAreaImpact.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'aId', title: '人员id',  hide: true},
            {field: 'aBh',title: '人员编号',  hide: true},
            {field: 'aXm',  sort: false, title: '姓名', fixed: true},
            {field: 'aDm',  sort: false,title: '档案所在店',width: 220},

            {field: 'dm',  sort: false,title: '指定店长的店(适用于情况2，其他情况指定店长店和档案所在店相等)',width: 220},
            {field: 'kqDz', sort: false, title: '考勤店长'},
            {field: 'kqDh', sort: false, title: '考勤店长电话',width: 200},

            {field: 'sfz', sort: false, title: '身份证',width: 300},
            {field: 'tel', sort: false, title: '电话',width: 200},
            {field: 'yhk', sort: false, title: '银行卡',width: 300},
            {field: 'yhkStatus', sort: false, title: '银行卡状态'},
            {field: 'cardName', sort: false, title: '银行名称',width: 200},

            {field: 'zt', sort: false, title: '人员状态'},
            {field: 'zw', sort: false, title: '职位'},
            {field: 'gz', sort: false, title: '工资'},
            {field: 'gw', sort: false, title: '岗位工资'},

            {field: 'qy', sort: false, title: '区域'},
            {field: 'eqy', sort: false, title: '二级区域'},
            {field: 'aBz', sort: false, title: '备注'},

            {field: 'yyjl', sort: false, title: '营运经理'},
            {field: 'dd', sort: false, title: '督导'},
            {field: 'xm', sort: false, title: '大店长'},
            {field: 'bz', sort: false, title: '店表的备注'},
            {align: 'center',width: 300, toolbar: '#tableBar', title: '操作', fixed: "right", style: 'text-align: right'}
        ]];
    };




    /**
     * 点击查询按钮
     */
    AttendAuditBtyAreaImpact.search = function () {
        var queryData = {};
        var attendMonth= $("#month").val();
        if(attendMonth){
            queryData['attendMonth'] = attendMonth.replace("-","");
        }else{
            //当前月份
            var thisdate = new Date();
            if(thisdate.getDate()<lastday){
                thisdate.setMonth(thisdate.getMonth()-1);
            }
            let monthNum = thisdate.getMonth() + 1;
            const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);
            queryData['attendMonth'] = month.replace("-","");
            $("#month").val(month);
        }

        queryData['impactType'] = $("#impactType").val();
        table.reload(AttendAuditBtyAreaImpact.tableId, {
            where: queryData, page: {curr: 1}
        });
        AttendAuditBtyAreaImpact.searchSumValue($("#month").val().replace("-",""));
    };
    /**
     * 点击查询合计值
     */
    AttendAuditBtyAreaImpact.searchSumValue = function (month) {
        var queryData = {};
        //queryData['attendMonth'] = month.replace("-","");
        $.ajax({
            type: "post",
            url: "/attendAuditBtyArea/searchSumValueImpact",
            datatype: "json",
            async: false,
            data:queryData,
            success: function (data) {
                console.log(data);
                if(data){

                    if(data.noDz){
                        $("#noDzSize").html(data.noDz);
                    }
                    if(data.dzNotInDm){
                        $("#dzNotInDmSize").html(data.dzNotInDm);
                    }


                    if(data.sfzHaveTwo){
                        $("#sfzHaveTwoSize").html(data.sfzHaveTwo);
                    }
                    if(data.telHaveTwo){
                        $("#telHaveTwoSize").html(data.telHaveTwo);
                    }
                    if(data.yhkHaveTwo){
                        $("#yhkHaveTwoSize").html(data.yhkHaveTwo);
                    }

                    if(data.zwNotInZw){
                        $("#zwNotInZwSize").html(data.zwNotInZw);
                    }
                    if(data.zwZSGzGwNotHundred){
                        $("#zwZSGzGwNotHundredSize").html(data.zwZSGzGwNotHundred);
                    }
                    if(data.zwJzZdgGwNotZero){
                        $("#zwJzZdgGwNotZeroSize").html(data.zwJzZdgGwNotZero);
                    }
                    if(data.zwJzNotHava){
                        $("#zwJzNotHavaSize").html(data.zwJzNotHava);
                    }
                    if(data.zwZdgNotHava){
                        $("#zwZdgNotHavaSize").html(data.zwZdgNotHava);
                    }
                    if(data.ddYyjlZtOne){
                        $("#ddYyjlZtOneSize").html(data.ddYyjlZtOne);
                    }


                }
            }
        });

    }
    /**
     * 获取是否允许考勤
     *
     * @param 获取是否允许考勤
     */
    AttendAuditBtyAreaImpact.getCreateAttendButton = function () {
        $.ajax({
            type: "post",
            url: "/attendAudit/getCreateAttendButton",
            datatype: "json",
            async: false,
            success: function (data) {
                if(data){
                    if(data.data.buttonValid == "0"){
                        buttonValid = true;
                        window.buttonValid = true;
                    }else{
                        buttonValid = false;
                        window.buttonValid = false;
                    }
                }
                console.log(data);
            }
        });
    };

    /**
     * 获取营运经理的列表
     *
     * @param 营运经理经理的列表
     */
    AttendAuditBtyAreaImpact.getYyjlList = function () {
        $.ajax({
            type: "post",
            url: "/attendAuditBtyArea/getYyjlList",
            datatype: "json",
            async: false,
            success: function (data) {

                //yyjl
                //初始化供货商
                $("#yyjl").html("<option value=''>全部营运经理</option>");
                $.each(data.data, function (index, val) {
                    $("#yyjl").append("<option value='" + val.yyjl + "'>" + val.yyjl+ "</option>");
                });
                form.render('select');
            }
        });
    };
    /**
     * 跳转到添加页面
     */
    AttendAuditBtyAreaImpact.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/attendAuditBtyArea/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    AttendAuditBtyAreaImpact.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/attendAuditBtyArea/edit?id=' + data.id
    };

    /**
     * 督导审核通过强制驳回，督导驳回
     * @param data
     */
    AttendAuditBtyAreaImpact.auditStatusToSubmitAudit=function(data){
        layer.open({
            type: 1,
            area: ['200px', '200px'],
            btnAlign: 'c',
            btn: ['确认', '取消'],
            content: '<p style="text-align: center;padding-top: 40px;">强制驳回到督导审核状态吗？</p>'
            ,yes: function(index, layero){
                var newData =data;
                $.ajax({
                    type: "post",
                    url: "/attendAuditBtyArea/auditStatusToSubmitAudit",
                    datatype: "json",
                    async: false,
                    data:data,
                    success: function (data) {
                        AttendAuditBtyAreaImpact.search();
                        Feng.success("成功!");
                        console.log(data);
                    }
                });
                //aaid

                layer.close(index);
            }
            ,btn2: function(index, layero){
                //按钮【按钮二】的回调
                //return false 开启该代码可禁止点击该按钮关闭
                layer.close(index);
            }
        });

    }

    /**
     * 考勤期间，未提交状态，更换店长
     *
     * @param 获取是否允许考勤
     */
    AttendAuditBtyAreaImpact.editDz = function(data){
        layer.open({
            type: 1,
            area: ['200px', '200px'],
            btnAlign: 'c',
            btn: ['确认', '取消'],
            content: '<p style="text-align: center;padding-top: 40px;">强制驳回到督导审核状态吗？</p>'
            ,yes: function(index, layero){


            }
            ,btn2: function(index, layero){
                //按钮【按钮二】的回调
                //return false 开启该代码可禁止点击该按钮关闭
                layer.close(index);
            }
        });
    }


    /**
     * 导出excel按钮
     */
    AttendAuditBtyAreaImpact.exportExcel = function () {
        var checkRows = table.checkStatus(AttendAuditBtyAreaImpact.tableId);
        if (checkRows.data.length === 0) {

            var queryData = {};
            var attendMonth= $("#month").val();
            if(attendMonth){
                queryData['attendMonth'] = attendMonth.replace("-","");
            }else{
                //当前月份
                var thisdate = new Date();
                if(thisdate.getDate()<lastday){
                    thisdate.setMonth(thisdate.getMonth()-1);
                }
                let monthNum = thisdate.getMonth() + 1;
                const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);
                queryData['attendMonth'] = month.replace("-","");
                $("#month").val(month);
            }
            queryData['impactType'] = $("#impactType").val();

            $.ajax({
                type: "post",
                url: "/attendAuditBtyArea/listAttendAuditForImpactForExport",
                datatype: "json",
                async: false,
                data:queryData,
                success: function (data) {
                    lists = data.data;

                    for (index in lists) {
                        var sfz = lists[index].sfz;
                        //支付编码
                        lists[index].sfz = sfz + "\t";

                        var yhk = lists[index].yhk;
                        //支付编码
                        lists[index].yhk = yhk + "\t";
                    }

                    table.exportFile(tableResult.config.id, lists, 'xls');
                }
            });



        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    AttendAuditBtyAreaImpact.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/attendAuditBtyArea/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(AttendAuditBtyAreaImpact.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     *  情况2，将该店考勤店长取消考勤店长权限
     */
    AttendAuditBtyAreaImpact.btnDzNotInDm = function (data) {
        $.ajax({
            type: "post",
            url: "/attendAuditBtyArea/queryDzNotInDm",
            datatype: "json",
            async: false,
            success: function (data) {
                AttendAuditBtyAreaImpact.search();
                debugger;
                Feng.success(data.msg);
            }
        });
    }
    // 渲染表格
    // var tableResult = table.render({
    //     elem: '#' + AttendAuditBtyArea.tableId,
    //     url: Feng.ctxPath + '/attendAuditBtyArea/list',
    //     page: true,
    //     height: "full-158",
    //     cellMinWidth: 100,
    //     cols: AttendAuditBtyArea.initColumn()
    // });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + AttendAuditBtyAreaImpact.tableId,
        url: Feng.ctxPath + '/attendAuditBtyArea/pagelistAttendAuditForImpact',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: AttendAuditBtyAreaImpact.initColumn()
    });



    // // 渲染表格
    // var tableResultNoDz = table.render({
    //     elem: '#' + AttendAuditBtyAreaNoDz.tableId,
    //     url: Feng.ctxPath + '/attendAuditBtyArea/listAttendAuditForImpactNoDz',
    //     page: true,
    //     height: "full-158",
    //     cellMinWidth: 100,
    //     cols: AttendAuditBtyArea.initColumn()
    // });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        AttendAuditBtyAreaImpact.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        AttendAuditBtyAreaImpact.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        AttendAuditBtyAreaImpact.exportExcel();
    });

    // 情况2，将该店考勤店长取消考勤店长权限
    $('#btnDzNotInDm').click(function () {
        AttendAuditBtyAreaImpact.btnDzNotInDm();
    });




    // 工具条点击事件
    table.on('tool(' + AttendAuditBtyAreaImpact.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            AttendAuditBtyAreaImpact.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            AttendAuditBtyAreaImpact.onDeleteItem(data);
        } else if(layEvent === 'auditStatusToSubmitAudit'){
            AttendAuditBtyAreaImpact.auditStatusToSubmitAudit(data);
        } else if(layEvent === 'editDz'){
            AttendAuditBtyAreaImpact.editDz(data);
        }
    });

    //初始化页面
    $(function () {
        // AttendAuditBtyAreaImpact.getCreateAttendButton();
        // AttendAuditBtyAreaImpact.getYyjlList();
        //var attendMonth = $("#month").val();
        //AttendAudit.search();
        setTimeout(() =>{
            AttendAuditBtyAreaImpact.search();
        },100);

    });



    $('#backupPage').click(function(){
        window.location.href = Feng.ctxPath + '/attendAuditBtyArea'
    });
});
